package com.ssy.lingxi.purchase.controller.tender;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.purchase.api.dto.request.tender.EvaluationTenderQueryRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.ExpertEvaluationTenderRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.ExpertExtractRecordQueryRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.ExpertExtractStatusRequest;
import com.ssy.lingxi.purchase.api.dto.response.tender.*;
import com.ssy.lingxi.purchase.api.enums.tender.ExpertExtractStatusEnum;
import com.ssy.lingxi.purchase.api.enums.tender.InviteTenderInStatusEnum;
import com.ssy.lingxi.purchase.api.enums.tender.InviteTenderOutStatusEnum;
import com.ssy.lingxi.purchase.entity.tender.*;
import com.ssy.lingxi.purchase.service.tender.IExpertExtractRecordService;
import com.ssy.lingxi.purchase.service.tender.IInviteTenderService;
import com.ssy.lingxi.purchase.service.tender.ITemplateService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @menu: 招投标-专家抽取通知管理
 * 专家抽取通知管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/3/20
 */
@RestController
@RequestMapping("/purchase/expertExtractRecord")
public class ExpertExtractRecordController extends BaseController {

    @Resource
    private IExpertExtractRecordService expertExtractRecordService;

    @Resource
    private IInviteTenderService inviteTenderService;

    @Resource
    private ITemplateService templateService;

    /**
     * 查询专家抽取通知列表
     * @param pageVO 分页实体
     * @param expertExtractRecordQueryRequest 评标专家库
     */
    @RequestMapping(value = "/getExpertExtractRecordList", method = RequestMethod.GET)
    public Wrapper<PageData<ExpertExtractRecordResponse>> getExpertExtractRecordList(PageVO pageVO, ExpertExtractRecordQueryRequest expertExtractRecordQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<ExpertExtractRecord> result = expertExtractRecordService.getExpertExtractRecordList(sysUser, pageVO, expertExtractRecordQueryRequest);
        List<ExpertExtractRecordResponse> collect = result.getContent().stream().map(existExpertExtractRecord -> {
            ExpertExtractRecordResponse expertExtractRecord = new ExpertExtractRecordResponse();
            expertExtractRecord.setId(existExpertExtractRecord.getId());
            expertExtractRecord.setStatus(existExpertExtractRecord.getStatus());
            InviteTender inviteTender = existExpertExtractRecord.getExpertExtract().getInviteTender();
            expertExtractRecord.setInviteTender(BeanUtil.copyProperties(inviteTender, ExpertExtractInviteTenderResponse.class));
            return expertExtractRecord;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), collect));
    }

    /**
     * 查询专家抽取通知信息
     * @param id 专家抽取通知id
     */
    @RequestMapping(value = "/getExpertExtractRecord", method = RequestMethod.GET)
    public Wrapper<ExpertExtractDetailSimpleResponse> getExpertExtractRecord(@RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        ExpertExtractRecord expertExtractRecord = expertExtractRecordService.getExpertExtractRecord(sysUser, id);
        if(expertExtractRecord != null){
            ExpertExtract expertExtract = expertExtractRecord.getExpertExtract();
            ExpertExtractDetailSimpleResponse expertExtractRecordDetail = new ExpertExtractDetailSimpleResponse();
            expertExtractRecordDetail.setId(expertExtractRecord.getId());
            expertExtractRecordDetail.setStatus(expertExtract.getStatus());
            InviteTender inviteTender = expertExtract.getInviteTender();
            ExpertExtractInviteTenderDetailResponse expertExtractInviteTenderDetail = BeanUtil.copyProperties(inviteTender, ExpertExtractInviteTenderDetailResponse.class);
            if(expertExtractInviteTenderDetail != null){
                //查询模板名称
                Long templateId = inviteTender.getTemplateId();
                if(templateId != null && templateId > 0){
                    Template template = templateService.getTemplate(templateId);
                    if(template != null){
                        expertExtractInviteTenderDetail.setTemplateName(template.getName());
                    }
                }
                expertExtractInviteTenderDetail.setInviteTenderInStatusValue(InviteTenderInStatusEnum.getMessage(inviteTender.getInviteTenderInStatus()));
                expertExtractInviteTenderDetail.setInviteTenderOutStatusValue(InviteTenderOutStatusEnum.getMessage(inviteTender.getInviteTenderOutStatus()));
                expertExtractRecordDetail.setInviteTender(expertExtractInviteTenderDetail);
            }
            return Wrapper.success(expertExtractRecordDetail);
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 确认抽取通知
     * @param expertExtractStatusRequest 评标专家库实体
     */
    @RequestMapping(value = "/updateExpertExtractRecordStatus", method = RequestMethod.POST)
    public Wrapper<Boolean> updateExpertExtractRecordStatus(@RequestBody ExpertExtractStatusRequest expertExtractStatusRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(expertExtractRecordService.updateExpertExtractStatus(sysUser, expertExtractStatusRequest));
    }

    /**
     * 查询待专家评标列表
     * @param pageVO 分页实体
     * @param evaluationTenderQueryRequest 评标专家库
     */
    @RequestMapping(value = "/getEvaluationTenderList", method = RequestMethod.GET)
    public Wrapper<PageData<ExpertExtractRecordResponse>> getEvaluationTenderList(PageVO pageVO, EvaluationTenderQueryRequest evaluationTenderQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        //状态：已确认的才可以评标
        ExpertExtractRecordQueryRequest expertExtractRecordQueryRequest = BeanUtil.copyProperties(evaluationTenderQueryRequest, ExpertExtractRecordQueryRequest.class);
        expertExtractRecordQueryRequest.setStatus(ExpertExtractStatusEnum.Confirm.getCode());
        expertExtractRecordQueryRequest.setIsOnlineEvaluation(true);
        Page<ExpertExtractRecord> result = expertExtractRecordService.getExpertExtractRecordList(sysUser, pageVO, expertExtractRecordQueryRequest);
        List<ExpertExtractRecordResponse> resultList = result.getContent().stream().map(expertExtractRecord -> {
            ExpertExtractRecordResponse expertExtractRecordResponse = new ExpertExtractRecordResponse();
            expertExtractRecordResponse.setId(expertExtractRecord.getId());
            expertExtractRecordResponse.setStatus(expertExtractRecord.getStatus());
            InviteTender inviteTender = expertExtractRecord.getExpertExtract().getInviteTender();
            ExpertExtractInviteTenderResponse inviteTenderResponse = BeanUtil.copyProperties(inviteTender, ExpertExtractInviteTenderResponse.class);
            inviteTenderResponse.setInviteTenderOutStatusValue(InviteTenderOutStatusEnum.getMessage(inviteTender.getInviteTenderOutStatus()));
            expertExtractRecordResponse.setInviteTender(inviteTenderResponse);
            return expertExtractRecordResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询专家评标信息
     * @param id 评标抽取记录id
     */
    @RequestMapping(value = "/getEvaluationTender", method = RequestMethod.GET)
    public Wrapper<ExpertEvaluationTenderResponse> getEvaluationTender(HttpServletRequest request, @RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        ExpertExtractRecord expertExtractRecord = expertExtractRecordService.getExpertExtractRecord(sysUser, id);
        if(expertExtractRecord != null){
            ExpertEvaluationTenderResponse expertEvaluationTender = this.modelMapper.map(expertExtractRecord, ExpertEvaluationTenderResponse.class);
            expertEvaluationTender.setId(expertExtractRecord.getId());
            InviteTender inviteTender = expertExtractRecord.getExpertExtract().getInviteTender();
            ExpertExtractRecordInviteTenderResponse expertExtractRecordInviteTender = BeanUtil.copyProperties(inviteTender, ExpertExtractRecordInviteTenderResponse.class);
            //查询模板名称
            Long templateId = inviteTender.getTemplateId();
            if(templateId != null && templateId > 0){
                Template template = templateService.getTemplate(templateId);
                if(template != null){
                    expertExtractRecordInviteTender.setTemplateName(template.getName());
                }
            }
            expertExtractRecordInviteTender.setInviteTenderInStatusValue(InviteTenderInStatusEnum.getMessage(inviteTender.getInviteTenderInStatus()));
            expertExtractRecordInviteTender.setInviteTenderOutStatusValue(InviteTenderOutStatusEnum.getMessage(inviteTender.getInviteTenderOutStatus()));
            expertEvaluationTender.setInviteTender(expertExtractRecordInviteTender);
            return Wrapper.success(expertEvaluationTender);
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 查询投标会员
     * @param inviteTenderId 招标id
     */
    @RequestMapping(value = "/getSubmitTenderMemberList", method = RequestMethod.GET)
    public Wrapper<List<EvaluationInviteTenderMemberSimpleResponse>> getSubmitTenderMemberList(@RequestParam("inviteTenderId") Long inviteTenderId) {
        InviteTender inviteTender = inviteTenderService.getInviteTender(inviteTenderId);
        if(inviteTender != null){
            //遍历查找有资格投标的会员和已经投标的会员
            List<InviteTenderMember> memberList = inviteTender.getMemberList();
            List<EvaluationInviteTenderMemberSimpleResponse> resultList = memberList.stream().map(inviteTenderMember -> {
                Boolean isSubmit = inviteTenderMember.getIsSubmit();
                if (isSubmit != null && isSubmit) {
                    return BeanUtil.copyProperties(inviteTenderMember, EvaluationInviteTenderMemberSimpleResponse.class);
                } else {
                    return null;
                }
            }).filter(Objects::nonNull).collect(Collectors.toList());
            return Wrapper.success(resultList);
        }else{
            throw new BusinessException(ResponseCode.PURCHASE_INVITE_TENDER_NOT_EXIST);
        }
    }

    /**
     * 专家评标
     */
    @RequestMapping(value = "/evaluationTender", method = RequestMethod.POST)
    public Wrapper<String> evaluationTender(@RequestBody ExpertEvaluationTenderRequest expertEvaluationTenderRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(expertExtractRecordService.evaluationTender(sysUser, expertEvaluationTenderRequest));
    }
}
